local hx__zhanjue_chen = fk.CreateSkill {
  name = "hx__zhanjue_chen",
  tags = {  },
}


hx__zhanjue_chen:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  if player:getMark("@hx__zhanjue_chen_zhanyi") >= 25 and not player:hasSkill("hx__zhenqiao_chen") then
    room:handleAddLoseSkills(player, "hx__zhenqiao_chen")
  end
  if player:getMark("@hx__zhanjue_chen_zhanyi") >= 50 and not player:hasSkill("hx__rende_chen") then
    room:handleAddLoseSkills(player, "hx__rende_chen")
  end
  if player:getMark("@hx__zhanjue_chen_zhanyi") >= 75 and not player:hasSkill("hx__fangquan_chen") then
    room:handleAddLoseSkills(player, "hx__fangquan_chen")
  end
  if player:getMark("@hx__zhanjue_chen_zhanyi") >= 99 and not player:hasSkill("hx__yanzhan") then
    room:handleAddLoseSkills(player, "hx__yanzhan")
  end
end)

hx__zhanjue_chen:addLoseEffect(function (self, player, is_death)
  local room = player.room
  room:setPlayerMark(player, "@hx__zhanjue_chen_zhanyi", 0)
  room:handleAddLoseSkills(player, "-hx__zhenqiao_chen|-hx__rende_chen|-hx__fangquan_chen|-hx__yanzhan")
end)

local function Changezhanyi(player, num)
  local room = player.room
  local zhanyi = player:getMark("@hx__zhanjue_chen_zhanyi")
  num = math.min(100 - zhanyi, num)
  if num > 0 then
    room:setPlayerMark(player, "@hx__zhanjue_chen_zhanyi", zhanyi + num)
    if player:getMark("@hx__zhanjue_chen_zhanyi") >= 25 and not player:hasSkill("hx__zhenqiao_chen") then
      room:handleAddLoseSkills(player, "hx__zhenqiao_chen")
    end
    if player:getMark("@hx__zhanjue_chen_zhanyi") >= 50 and not player:hasSkill("hx__rende_chen") then
      room:handleAddLoseSkills(player, "hx__rende_chen")
    end
    if player:getMark("@hx__zhanjue_chen_zhanyi") >= 75 and not player:hasSkill("hx__fangquan_chen") then
      room:handleAddLoseSkills(player, "hx__fangquan_chen")
    end
    if player:getMark("@hx__zhanjue_chen_zhanyi") >= 100 and not player:hasSkill("hx__yanzhan") then
      room:handleAddLoseSkills(player, "hx__yanzhan")
    end
  end
end

hx__zhanjue_chen:addEffect(fk.GameStart, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(hx__zhanjue_chen.name) and player:getMark("@hx__zhanjue_chen_zhanyi") < 100
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local num = 20
    if player:hasSkill("hx__kangxiang") and table.find(room.alive_players, function(p)
      return p ~= player and p.kingdom == "shu"
    end)
    then
      num = 60
      player:broadcastSkillInvoke("hx__kangxiang")
      room:notifySkillInvoked(player, "hx__kangxiang", "support")
    end
    Changezhanyi(player, num)
  end,
})
hx__zhanjue_chen:addEffect(fk.Damaged, {
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(hx__zhanjue_chen.name) and player:getMark("@hx__zhanjue_chen_zhanyi") < 100
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    Changezhanyi(player, 10 * data.damage)
  end,
})
hx__zhanjue_chen:addEffect(fk.Damage, {
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(hx__zhanjue_chen.name) and player:getMark("@hx__zhanjue_chen_zhanyi") < 100
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    Changezhanyi(player, 15 * data.damage)
  end,
})
hx__zhanjue_chen:addEffect(fk.AfterCardsMove, {
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(hx__zhanjue_chen.name) and player:getMark("@hx__zhanjue_chen_zhanyi") < 100 then
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Card.PlayerHand then
          return true
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    Changezhanyi(player, 5)
  end,
})


return hx__zhanjue_chen